﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using PDFDesign.Model.Const;
using System;

namespace PDFDesign.Model
{
    /// <summary>
    /// 页面设置
    /// </summary>
    public class PageSetting
    {
        private object size;
        /// <summary>
        /// 页面大小
        /// </summary>
        [JsonProperty("size")]
        public JToken Size
        {
            get
            {
                if (SizeArray == null || SizeArray.Length == 0)
                    return SizeMode.ToString();
                return JToken.FromObject(SizeArray);
            }
            set
            {
                try
                {
                    var type = value.GetType().FullName;
                    if (type == "System.String" || type == "Newtonsoft.Json.Linq.JValue")
                    {
                        var mode = PDFSize.A4;
                        Enum.TryParse(value.ToString(), out mode);
                        SizeMode = mode;
                    }
                    else
                    {
                        SizeArray = ((JArray)value).ToObject<float[]>();
                    }
                }
                finally
                {
                    size = value;
                }
            }
        }
        /// <summary>
        /// 页面大小数组
        /// </summary>
        [JsonIgnore]
        public float[] SizeArray { get; set; }
        /// <summary>
        /// 页面大小模式
        /// </summary>
        [JsonIgnore]
        [JsonConverter(typeof(StringEnumConverter))]
        public PDFSize SizeMode { get; set; }
        /// <summary>
        /// 布局方式
        /// </summary>
        [JsonProperty("layout")]
        [JsonConverter(typeof(StringEnumConverter))]
        public Layout Layout { get; set; }
        /// <summary>
        /// 字体大小
        /// </summary>
        [JsonProperty("fontSize")]
        public int FontSize { get; set; }
        /// <summary>
        /// 每页报表数量
        /// </summary>
        [JsonProperty("reportCount")]
        public int ReportCount { get; set; } = 1;
        /// <summary>
        /// 是否显示页码
        /// </summary>
        [JsonProperty("showPageNum")]
        public bool ShowPageNum { get; set; }
    }
}